package top.lethelh.oa.service;

import top.lethelh.oa.dao.RbacDao;
import top.lethelh.oa.dao.UserDao;
import top.lethelh.oa.entity.Node;
import top.lethelh.oa.entity.User;
import top.lethelh.oa.service.exception.BusinessException;
import top.lethelh.oa.utils.MD5Utils;

import java.util.List;

/**
 * UserService
 * @author jasonper
 */
public class UserService {
    private UserDao userDAO = new UserDao();
    private RbacDao rbacDAO = new RbacDao();

    /**
     * 根据前台输入进行登陆校验
     * @param username 用户名
     * @param password 密码
     * @return User对象
     */
    public User checkLogin(String username, String password){
        User user = userDAO.selectByUsername(username);
        if (user == null){
            //抛出用户不存在异常
            throw new BusinessException("L001", "用户名不存在");
        }
        //对前台输入的密码进行加盐混淆后生成MD5，与保存在数据库的MD5密码进行比对
        String md5 = MD5Utils.md5Digest(password, user.getSalt());

        if (!md5.equals(user.getPassword())){
            throw new BusinessException("L002", "密码错误");
        }
        return user;
    }
    public List<Node> selectNodeByUserId(Long userId){
        List<Node> nodeList = rbacDAO.selectNodeByUserId(userId);
        return nodeList;
    }
}
